package com.singfan.common.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间相关的工具类
 * Author: HanSongHe
 * Date: 2015-07-27 15:37
 * Desc:
 */
public class TimeUtil {
    /**
     * 判断时间是否在今天之前
     * @param date 要判断的时间
     * @return  true:在今天之前  false:不在今天之前
     */
    public static boolean beforeToday(Date date){
        boolean result = false;

        long todayStartTime = getTodayStartTime();
        if(date.getTime() < todayStartTime){
            result = true;
        }

        return result;
    }

    /**
     * 判断时间是否在今天之后
     * @param date 要判断的时间
     * @return  true:在今天之后  false:不在今天之后
     */
    public static boolean afterToday(Date date){
        boolean result = false;

        long tomorrowStartTime = getTomorrowStartTime();
        if(date.getTime() > tomorrowStartTime){
            result = true;
        }

        return result;
    }

    /**
     * 判断时间是否为今天
     * @param date 要判断的时间
     * @return true:是昨天    false:不是
     */
    public static boolean isToday(Date date){
        boolean result = false;

        long todayStartTime = getTodayStartTime();
        long tomorrowStartTime = getTomorrowStartTime();
        if(date.getTime() > todayStartTime && date.getTime() < tomorrowStartTime){
            result = true;
        }

        return result;
    }

    /**
     * 获取明天的起始时间
     * @return 明天的起始时间
     */
    public static long getTomorrowStartTime(){
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH,1);
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH);
        now.set(year,month,day,0,0,0);

        return now.getTime().getTime();
    }

    /**
     * 获取年月日的简单形式 例：20140521
     * @param time 时间
     * @return 年月日的简单形式
     */
    public static String getTearMonthDayText(long time){
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return "" + year + formatMonthOrDayWith0(month) + formatMonthOrDayWith0(day);
    }

    /**
     * 获取年月日的简单形式 例：20140521
     * @param time 时间
     * @return 年月日的简单形式
     */
    public static int getTearMonthDayValue(long time){
        String yearMonthDayText = getTearMonthDayText(time);
        return Integer.parseInt(yearMonthDayText);
    }

    /**
     * 获取年月日的简单形式 例：20140521
     * @return 年月日的简单形式
     */
    public static String getTearMonthDayText(){
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        return "" + year + formatMonthOrDayWith0(month) + formatMonthOrDayWith0(day);
    }

    /**
     * 获取年月日的简单形式 例：20140521
     * @return 年月日的简单形式
     */
    public static int getTearMonthDayValue(){
        String yearMonthDayText = getTearMonthDayText();
        return Integer.parseInt(yearMonthDayText);
    }

    /**
     * 获取昨天的起始时间
     * @return 昨天的起始时间
     */
    public static long getYesterdayStartTime(){
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -1);
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH);
        now.set(year,month,day,0,0,0);

        return now.getTime().getTime();
    }

    /**
     * 获取今天的起始时间
     * @return 今天的起始时间
     */
    public static long getTodayStartTime(){
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int day = now.get(Calendar.DAY_OF_MONTH);
        now.set(year,month,day,0,0,0);

        return now.getTime().getTime();
    }

    private static String formatMonthOrDayWith0(int monthOrDay){
        if(monthOrDay < 10){
            return "0" + monthOrDay;
        }
        else{
            return "" + monthOrDay;
        }
    }
}
